package com.tycmc.signelectroniccontracts.act.htong;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.read.ActReadPDF;
import com.tycmc.signelectroniccontracts.entity.HtEntity;
import com.tycmc.signelectroniccontracts.logic.htong.PreHtList;
import com.tycmc.signelectroniccontracts.tools.adapter.abslistview.AdapterHelper;
import com.tycmc.signelectroniccontracts.tools.adapter.abslistview.GeneralAdapter;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.manager.AppManager;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.ui.PullToRefreshLayout;
import com.tycmc.signelectroniccontracts.ui.listview.PullableListView;

import java.util.Collection;
import java.util.List;

public class ActHtList extends ActBase implements PreHtList.HtListInterface {

    private PreHtList pre;
    private GeneralAdapter<HtEntity> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_htlist);
        pre = new PreHtList(this);
        setTitleText("签约合同列表");
        initListView();
        initView();
    }

    private long cutime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - cutime > 2000) {
                cutime = System.currentTimeMillis();
                ToastShow.popupToast("再按一次退出");
            } else {
                AppManager.getAppManager().AppExit(this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (TyTypenum.isQianyue) {
            pre.postData(findViewById(((RadioGroup) findViewById(R.id.rg_ht)).getCheckedRadioButtonId()).getTag() + "");
            TyTypenum.isQianyue = false;
        }
//        ( (PullToRefreshLayout) findViewById(R.id.pullLayout)).autoRefresh();
    }

    private void initView() {
        ((RadioGroup) findViewById(R.id.rg_ht)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                pre.postData(findViewById(checkedId).getTag() + "");

            }
        });
        ((RadioGroup) findViewById(R.id.rg_ht)).check(R.id.rb_daiqianyue);
        findViewById(R.id.iv_back).setVisibility(View.GONE);
    }

    private void initListView() {
        PullToRefreshLayout layout = (PullToRefreshLayout) findViewById(R.id.pullLayout);

        layout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pre.postData(findViewById(((RadioGroup) findViewById(R.id.rg_ht)).getCheckedRadioButtonId()).getTag() + "");
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.loadmoreFinish(0);
            }
        });
        PullableListView listView = (PullableListView) findViewById(R.id.pullListView);
        listView.setCancelPullUp(false);
        adapter = new GeneralAdapter<HtEntity>(this, R.layout.item_hetong_list) {
            @Override
            public void convert(AdapterHelper helper, final HtEntity item) {
                //客户待签约 公司待签约 客户签约公司待签约  签约成功
                helper.setText(R.id.tv_htongbianhao, item.getContractType());
                helper.setText(R.id.tv_htongriqi, "(" + item.getContractNo() + ")");
                helper.setAdapter(R.id.custListview, new GeneralAdapter<HtEntity.SubContractListEntity>(context, R.layout.item_contractlist_childer, item.getSubList()) {
                    @Override
                    public void convert(AdapterHelper helper, final HtEntity.SubContractListEntity item1) {
                        helper.setText(R.id.tv_hetongname, item1.getContractName());
                        helper.setText(R.id.tv_qianyue, item1.getSignStatus());
                        helper.setTextColor(R.id.tv_qianyue, getResources().getColor(item1.getSignStatusID().equals("2") ? R.color.colorhead : R.color.text_gray));
                        helper.setOnClickListener(R.id.tv_hetongname, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                start(new Intent(ActHtList.this, ActReadPDF.class)
                                        .putExtra("contractNo", item.getContractNo())
                                        .putExtra("subContractNo", item1.getSubContractNo())
                                        .putExtra("entity", item1)
                                );
                            }
                        });
                    }
                });
//                helper.setText(R.id.tv_gongkuang, "工矿产品购销合同" + (item.getClientTypeId().equals("01") ? "个人" : "法人"));
            }
        };
        listView.setAdapter(adapter);
    }

    private void refreashState(Collection<? extends Object> collection, PullToRefreshLayout pullLayout, boolean isAdd) {
        if (pullLayout == null) {
            return;
        }
        if (isAdd) {
            if (collection == null)
                pullLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
            else if (collection.size() == 0)
                pullLayout.loadmoreFinish(PullToRefreshLayout.NODATA);
            else
                pullLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);

        } else {
            if (collection == null)
                pullLayout.refreshFinish(PullToRefreshLayout.FAIL);
            else if (collection.size() == 0)
                pullLayout.refreshFinish(PullToRefreshLayout.NODATA);
            else
                pullLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
        }
    }

    @Override
    public void addAll(List<HtEntity> list, boolean isAdd) {
        refreashState(list, (PullToRefreshLayout) findViewById(R.id.pullLayout), isAdd);
        adapter.addAll(list, isAdd);
    }
}
